<?php
namespace zzcms\util;


/* * *********************************************
 * @类名:   page
 * @参数:   $total_count - 总记录数
 *          $pagesize - 一页显示的记录数
 *          $current_page - 当前页
 *          $current_url - 获取当前的url
 * @功能:   分页实现
 */

class Page
{

    private $total_count;             //总记录数
    private $pagesize;                //一页显示的记录数
    private $current_page;           //当前页
    private $current_page_count;    //总页数
    private $own_i;                  //起头页数
    private $own_en;                 //结尾页数
    private $current_url;           //获取当前的url

    /*
     * $show_pages
     * 页面显示的格式，显示链接的页数为2*$show_pages+1。
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */

    public function __construct($total_count, $pagesize = 20, $current_page = 1, $current_url, $show_pages = 3)
    {
        $this->total_count = $this->numeric($total_count);
        $this->pagesize = $this->numeric($pagesize);
        $this->current_page = $this->numeric($current_page);
        $this->current_page_count = ceil($this->total_count / $this->pagesize);
        if (false === strpos($current_url, '?')) {
            $this->current_url = str_replace("/page/{$current_page}", '', $current_url) . '?page={page}';
        } else {
            $this->current_url = str_replace("/page/{$current_page}", '', $current_url) . '&page={page}';
        }
        if ($this->total_count < 0)
            $this->total_count = 0;
        if ($this->current_page < 1)
            $this->current_page = 1;
        if ($this->current_page_count < 1)
            $this->current_page_count = 1;
        if ($this->current_page > $this->current_page_count)
            $this->current_page = $this->current_page_count;
        $this->limit = ($this->current_page - 1) * $this->pagesize;
        $this->own_i = $this->current_page - $show_pages;
        $this->own_en = $this->current_page + $show_pages;
        if ($this->own_i < 1) {
            $this->own_en = $this->own_en + (1 - $this->own_i);
            $this->own_i = 1;
        }
        if ($this->own_en > $this->current_page_count) {
            $this->own_i = $this->own_i - ($this->own_en - $this->current_page_count);
            $this->own_en = $this->current_page_count;
        }
        if ($this->own_i < 1)
            $this->own_i = 1;
    }

    //检测是否为数字
    private function numeric($num)
    {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }

    //地址替换
    private function paramReplace($page)
    {
        return str_replace("{page}", $page, $this->current_url);
    }

    //首页
    private function firstPage()
    {
        if ($this->current_page != 1) {
            return "<a href='" . $this->paramReplace(1) . "' title='首页'>首页</a>";
        } else {
            return "<span>首页</span>";
        }
    }

    //上一页
    private function prevPage()
    {
        if ($this->current_page != 1) {
            return "<li><a href='" . $this->paramReplace($this->current_page - 1) . "' title='上一页'>上一页</a></li>";
        } else {
            return "<li><span>上一页</span></li>";
        }
    }

    //下一页
    private function nextPage()
    {
        if ($this->current_page != $this->current_page_count) {
            return "<li><a href='" . $this->paramReplace($this->current_page + 1) . "' title='下一页'>下一页</a></li>";
        } else {
            return "<li><span>下一页</span></li>";
        }
    }

    //尾页
    private function lastPage()
    {
        if ($this->current_page != $this->current_page_count) {
            return "<li><a href='" . $this->paramReplace($this->current_page_count) . "' title='尾页'>尾页</a></li>";
        } else {
            return "<li><span>尾页</span></li>";
        }
    }

    //输出
    public function paginate()
    {
        $str = '<ul class="pagination">';
        $str .= $this->firstPage();
        $str .= $this->prevPage();
        if ($this->own_i > 1) {
            $str .= "<li><span>...</span></li>";
        }
        for ($i = $this->own_i; $i <= $this->own_en; $i++) {
            if ($i == $this->current_page) {
                $str .= "<li class='active'><span>$i</span></li>";
            } else {
                $str .= "<li><a href='" . $this->paramReplace($i) . "' title='第" . $i . "页'>$i</a></li>";
            }
        }
        if ($this->own_en < $this->current_page_count) {
            $str .= "<li><span>...</span></li>";
        }
        $str .= $this->nextPage();
        $str .= $this->lastPage();
        $str .= "<li><span>共<b style='margin: 0 5px;'>" . $this->current_page_count . "</b>页<b style='margin: 0 5px;'>" . $this->total_count . "</b>条数据</span></li>";
        $str .= "</ul>";
        return $str;
    }
}